//
//  ViewController.m
//  UIPopoverListViewDemo
//
//  Created by su xinde on 13-3-13.
//  Copyright (c) 2013年 su xinde. All rights reserved.
//
#define ScreenWidth [UIScreen mainScreen].bounds.size.width
#define ScreenHeight [UIScreen mainScreen].bounds.size.height
#import "PopoverListViewController.h"
#import "clsSettingData.h"
@interface PopoverListViewController (){
    NSMutableArray* _Array;
    NSInteger _DefaultIndex;
    CallBack _closecallback;
    UIPopoverListView *_poplistview;
    NSTimer *_timer;
    int time;
}

@end

@implementation PopoverListViewController

- (void)ShowPopView:(NSString *)pTitle :(NSString *)BtnTitle :(NSMutableArray *)pArray :(NSInteger)pDefaultIndex :(CallBack)pCloseCallback
{
    _Array=pArray;
    _DefaultIndex=pDefaultIndex;
    _closecallback=pCloseCallback;
    time = [BtnTitle intValue];
    CGFloat xWidth = ScreenWidth - 50.0f*2;
    CGFloat yHeight = 100.0f + 35*_Array.count;
    CGFloat yOffset = (ScreenHeight - yHeight)/2.0f;
    _poplistview= [[UIPopoverListView alloc] initWithFrame:CGRectMake(50, yOffset, xWidth, yHeight)];
    _poplistview.delegate = self;
    _poplistview.datasource = self;
    _poplistview.listView.scrollEnabled = NO;
    [_poplistview setTitle:pTitle];
    NSString *title =[NSString stringWithFormat:@"%@%@",BtnTitle,@"秒重發"];
    [_poplistview setBtnTitle:title :NO];
    [_poplistview show];
    _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(ChangeBtnTitle:) userInfo:BtnTitle repeats:YES];
}

- (void)dealloc
{
    if (_poplistview) {
        _poplistview.delegate=nil;
        _poplistview.datasource=nil;
        _poplistview=nil;
    }
    _Array=nil;
    _closecallback=nil;
}



#pragma mark - UIPopoverListViewDataSource

- (UITableViewCell *)popoverListView:(UIPopoverListView *)popoverListView
                    cellForIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"PopoverListcell";
    

    UITableViewCell *cell = [popoverListView.listView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] ;
        
        cell.textLabel.font = [UIFont systemFontOfSize:14];
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
        cell.textLabel.textColor = [UIColor blackColor];
        cell.textLabel.backgroundColor = [UIColor clearColor];
        if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0f) {
            cell.backgroundColor = [UIColor clearColor];
        }
        UIView * v = [[UIView alloc] init];
        v.backgroundColor = [UIColor whiteColor];
        cell.selectedBackgroundView = v;
        
        CGSize newsize=CGSizeMake(38/2, 38/2);
        UIImageView *selectview = [[UIImageView alloc] initWithFrame:CGRectMake(_poplistview.frame.size.width -50 , (40-newsize.height)/2, newsize.width, newsize.height)];
        [selectview setContentMode:UIViewContentModeScaleToFill];
        selectview.tag=999;
        [cell.contentView addSubview:selectview];
    }
    clsSettingData *setting=[_Array objectAtIndex:indexPath.row];
    cell.textLabel.text = setting.Name;
    cell.textLabel.textColor=[UIColor orangeColor];
    cell.textLabel.font=[UIFont systemFontOfSize:16];
    return cell;
}

- (NSInteger)popoverListView:(UIPopoverListView *)popoverListView
       numberOfRowsInSection:(NSInteger)section
{
    return _Array.count;
}

- (CGFloat)popoverListView:(UIPopoverListView *)popoverListView
   heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 35;
}

#pragma mark - UIPopoverListViewDelegate

- (void)popoverListView:(UIPopoverListView *)popoverListView
     didSelectIndexPath:(NSString *)indexPath
{
    if ([indexPath isEqualToString:@""]) {
        return;
    }
    if (_closecallback) {
        _closecallback(indexPath);
        _closecallback=nil;
    }
    if (_poplistview) {
        _poplistview.delegate=nil;
        _poplistview.datasource=nil;
        _poplistview=nil;
    }
    _Array=nil;
    [_timer invalidate];
}

- (void)popoverListViewCancel:(UIPopoverListView *)popoverListView{
    if (_closecallback) {
        _closecallback(nil);
        _closecallback=nil;
    }
    if (_poplistview) {
        _poplistview.delegate=nil;
        _poplistview.datasource=nil;
        _poplistview=nil;
    }
    _Array=nil;
    [_timer invalidate];
}

-(void)ReBtn
{
    
}
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {

}
- (void)ChangeBtnTitle:(NSTimer *)timer
{
    time-=1;
    if (time==0) {
        [_timer invalidate];
        [_poplistview setBtnTitle:@"重新發送" :YES];
    }else{
        [_poplistview setBtnTitle:[NSString stringWithFormat:@"%d%@",time,@"秒重發"] :NO];
    }
}

@end
